﻿//abc中文
#include <iostream>
#include <locale>
#include <clocale>
#include <string>
#include <chrono>
#include <sstream>
#include <iomanip>
#include <codecvt>

using namespace std;


int main()
{


	char cs[] = "abc中文";
	char u8cs[] = u8"abc中文";
	wchar_t wcs[] = L"abc中文";
	char16_t u16cs[] = u"abc中文";
	char32_t u32cs[] = U"abc中文";

	cout << "Test string: " << cs << endl;

	cout.setf(ios::hex, ios::basefield);
	unsigned char *ptr = nullptr;
	ptr = reinterpret_cast<unsigned char*>(cs);
	cout << setw(12) << "char: ";
	cout.fill('0');
	for (int i = 0; i < sizeof(cs) - sizeof(char); ++i)
	{
		cout << setw(2) << static_cast<int>(*(ptr + i)) << "  ";
	}
	cout << endl;

	ptr = reinterpret_cast<unsigned char*>(u8cs);
	cout << setfill(' ') << setw(12) << "utf-8 char: ";
	cout.fill('0');
	for (int i = 0; i < sizeof(u8cs) - sizeof(char); ++i)
	{
		cout << setw(2) << static_cast<int>(*(ptr + i)) << "  ";
	}
	cout << endl;

	ptr = reinterpret_cast<unsigned char*>(wcs);
	cout << setfill(' ') << setw(12) << "wchar_t: ";
	cout.fill('0');
	for (int i = 0; i < sizeof(wcs) - sizeof(wchar_t); ++i)
	{
		cout << setw(2) << static_cast<int>(*(ptr + i)) << "  ";
	}
	cout << endl;

	ptr = reinterpret_cast<unsigned char*>(u16cs);
	cout << setfill(' ') << setw(12) << "char16_t: ";
	cout.fill('0');
	for (int i = 0; i < sizeof(u16cs) - sizeof(char16_t); ++i)
	{
		cout << setw(2) << static_cast<int>(*(ptr + i)) << "  ";
	}
	cout << endl;

	ptr = reinterpret_cast<unsigned char*>(u32cs);
	cout << setfill(' ') << setw(12) << "char32_t: ";
	cout.fill('0');
	for (int i = 0; i < sizeof(u32cs) - sizeof(char32_t); ++i)
	{
		cout << setw(2) << static_cast<int>(*(ptr + i)) << "  ";
	}
	cout << endl;

	cout << "\n----------------------------------\n" << endl;


	//system("chcp 65001");
	//system("chcp 936");
	system("chcp");

	char *str = "abc中文";
	char *u8str = u8"abc中文";
	wchar_t *wstr = L"abc中文";

	locale locC("C"); //默认编码
	locale locutf8 = locale("zh_cn.utf8");
	locale locZH("zh"); 

	locale::global(locC);
	cout << "LocC locale:\n";
	cout << "str: " << str << endl;
	cout << "u8str: " << u8str << endl;
	wcout << L"wstr: " << wstr << endl;
	wcout.clear();
	cout.clear();
	cout << "\n" << endl;

	locale::global(locutf8); 
	cout << "UTF-8 locale:\n";
	cout << "str: " << str << endl;
	cout << "u8str: " << u8str << endl;
	wcout << L"wstr: " << wstr << endl;
	wcout.clear();
	cout.clear();
	cout << endl;

	locale::global(locZH);
	cout << "locZH locale:\n";
	cout << "str: " << str << endl;
	cout << "u8str: " << u8str << endl;
	wcout << L"wstr: " << wstr << endl;
	wcout.clear();
	cout.clear();
	cout << endl;

	return 0;
}


